package doInterce.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import doInterce.DoInterface;
import utils.DBUtil;
import utils.PathUtil;
import utils.YmlUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 生成年度考核结果
 */
public class DoA14DataDeal implements DoInterface {
    public Map<String, String> run() throws Exception {
        Map<String, String> a14Z101Map = new HashMap<>();
        Map<String, Object> ymlMap = YmlUtil.getYmlMap();
        String url = Convert.toStr(ymlMap.get("url"));
        String userName = Convert.toStr(ymlMap.get("userName"));
        String password = Convert.toStr(ymlMap.get("password"));

        try(Connection conn = DBUtil.getConnection(url,userName,password)) {
            Statement statement = conn.createStatement();

            Map<String, List<Map<String, String>>> a14ListMap = new HashMap<>();

            ResultSet resultSet = statement.executeQuery("select A0000 from A14_ERROR_INFO");
            while (resultSet.next()) {
                String value = resultSet.getString("A0000");
                a14ListMap.put(value, new ArrayList<>());
            }
            resultSet.close();


            List<Map<String, String>> a14List = new ArrayList<>();
            resultSet = statement.executeQuery("select A0000,A1400,A1404A,A1404B,A1407,A1411A,A1415,A1424,A1428 from A14 where A0000 in(select A0000 from A14_ERROR_INFO) order by a0000,A1407");
            ResultSetMetaData metaData = resultSet.getMetaData();
            while (resultSet.next()) {
                a14List.add(DBUtil.getFieldMap(resultSet, metaData));
            }
            resultSet.close();

            //年度考核转成以人为单位
            Map<String, List<Map<String, String>>> a0000Map = a14List.stream().collect(Collectors.groupingBy(a14 -> a14.get("A0000")));

            for (String key : a0000Map.keySet()) {
                Iterator<Map<String, String>> iterator = a0000Map.get(key).iterator();
                while (iterator.hasNext()) {
                    Map<String, String> next = iterator.next();
                    String a1404a = next.get("A1404A");
                    String a1404b = next.get("A1404B");
                    String a1407 = next.get("A1407");

                    if (StrUtil.isBlank(a1404a) || StrUtil.isBlank(a1404b) || StrUtil.isBlank(a1407)) {
                        continue;
                    }

                    a1404a = a1404a.replaceAll(" ", "");
                    a1407 = a1407.replaceAll(" ", "").replaceAll("[.]", "");
                    if (StrUtil.isNotBlank(a1404a)) {
                        a1404a = a1404a.replaceAll(" ", "");
                    }
                    if ("01".equals(a1404b)) {
                        a1404b = "01119";
                    }
                    if ("01119".equals(a1404b) && "其他".equals(a1404a)) {
                        iterator.remove();
                        continue;
                    }

                    if (a1407.length() < 6) {
                        continue;
                    }

                    next.put("A1404A", a1404a);
                    next.put("A1404B", a1404b);
                    next.put("A1407", a1407);
                }
            }


            for (String key : a0000Map.keySet()) {
                List<Map<String, String>> eleList = a0000Map.get(key);
                //A0000
                String a0000 = eleList.get(0).get("A0000");
                String a14Z101 = "";
                for (int i = 0; i < eleList.size(); i++) {
                    Map<String, String> eleMap = eleList.get(i);
                    String a1404a = eleMap.get("A1404A");
                    String a1404b = eleMap.get("A1404B");
                    String a1407 = eleMap.get("A1407");
                    String a1411a = eleMap.get("A1411A");

                    a14Z101 = a14Z101 + getA14Z101(a0000, a1404a, a1404b, a1411a, a1407);

                    if (i == eleList.size() - 1) {
                        if (a14Z101.length() > 0) {
                            a14Z101 = a14Z101.substring(0, a14Z101.length() - 1) + "。";
                        }
                    }

                    a14Z101Map.put(a0000, a14Z101);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return a14Z101Map;
    }



    /**
     * 获取奖惩描述
     * @param a0000
     * @param a1404A
     * @param a1404B
     * @param a1411A
     * @param a1407
     * @return
     */
    public String getA14Z101(String a0000,String a1404A,String a1404B,String a1411A,String a1407){
        if (a1404B.equals("01111") || a1404B.equals("01112") || a1404B.equals("01113") || a1404B.equals("01114") || a1404B.equals("01115") || a1404B.equals("01119")){

            if(StrUtil.isBlank(a1411A) || "null".equals(a1411A)){
                return a1407.substring(0,4) + "." + a1407.substring(4,6) + "，" + "经批准，" + a1404A + "；";
            }
            else{
                return a1407.substring(0,4) + "." + a1407.substring(4,6) + "，" + "经" + a1411A + "批准，" + a1404A + "；";
            }

        }
        else {

            if(StrUtil.isBlank(a1411A) || "null".equals(a1411A)){
                return a1407.substring(0,4) + "." + a1407.substring(4,6) + "，" + "经批准，受" + a1404A + "处分；";
            }
            else{
                return a1407.substring(0,4) + "." + a1407.substring(4,6) + "，" + "经" + a1411A + "批准，受" + a1404A + "处分；";
            }

        }
    }


    @Override
    public void doExecute() {
        try {
            Map<String, String> run = run();
            List<String> sqlList = new ArrayList<>();
            for (String key : run.keySet()) {
                sqlList.add("update A01 set a14Z101='"+run.get(key)+"' where a0000='"+key+"';");
            }

            Map<String, Object> ymlMap = YmlUtil.getYmlMap();
            String path = PathUtil.getJarPath().replace("parsing_excel.jar", "");
            System.out.println(path);
            path = path + "sql.txt";
            FileUtil.writeLines(sqlList, path, "UTF-8");


        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
